﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Weixin.MP.DAL.WeixinMP;
using Weixin.MP.UI;
using Weixin.MP.Web.Filters;

namespace Weixin.MP.Web.Controllers
{
    public class UserController : BaseController
    {

        public ActionResult Index()
        {
            var pageIndex = Common.GetString("pageIndex").ToInt();
            var pageSize = Common.GetString("pageSize").ToInt();
            var recordCount = Common.GetString("recordCount").ToInt64();
            if (pageIndex <= 0) pageIndex = 1;
            if (pageSize <= 0) pageSize = Common.DefaultPageSize;

            var bo = new Weixin.MP.BLL.Users.UsersBO();
            var result = bo.GetUsers(pageSize, pageIndex, ref recordCount);

            ViewBag.PageIndex = pageIndex;
            ViewBag.PageSize = pageSize;
            ViewBag.RecordCount = recordCount;
            return View(result);

        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [DwzJsonException]
        public ActionResult AddUser(string userId, string password, string nickName, string phone, string email)
        {
            var bo = new Weixin.MP.BLL.Users.UsersBO();
            var u = bo.GetByUserId(userId);
            if (u != null)
            {
                return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new DwzJsonResult()
                {
                    StatusCode = DwzStatusCode.Error,
                    Message = string.Format("操作失败：{0}", "用户已存在！")
                }));
            }

            bo.AddUsers(new UsersEO()
            {
                UserId = userId,
                Password = password.ToMD5(),
                Email = email,
                NickName = nickName,
                Phone = phone
            });

            return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new DwzJsonResult()
            {
                StatusCode = DwzStatusCode.Ok,
                Message = string.Format("添加成功！"),
                NavTabId = "User_List",
                Rel = "",
                CallbackType = "closeCurrent"
            }));
        }
        [DwzJsonException]
        public ActionResult Edit(string userId)
        {
            var bo = new Weixin.MP.BLL.Users.UsersBO();
            var u = bo.GetByUserId(userId);
            if (u != null)
            {
                ViewBag.UserId = u.UserId;
                ViewBag.NickName = u.NickName;
                ViewBag.Email = u.Email;
                ViewBag.Phone = u.Phone;
            }
            return View(u);
        }

        [HttpPost]
        [DwzJsonException]
        public ActionResult EditUser(string userId, string password, string nickName, string phone, string email)
        {
            var bo = new Weixin.MP.BLL.Users.UsersBO();
            var u = bo.GetByUserId(userId);
            if (u == null)
            {
                return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new DwzJsonResult()
                {
                    StatusCode = DwzStatusCode.Error,
                    Message = string.Format("操作失败：{0}", "用户不存在！")
                }));
            }
            u.UserId = userId;
            if (!string.IsNullOrEmpty(password))
            {
                u.Password = password.ToMD5();
            }
            u.NickName = nickName;
            u.Phone = phone;
            u.Email = email;
            bo.EditUsers(u);

            return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new DwzJsonResult()
            {
                StatusCode = DwzStatusCode.Ok,
                Message = string.Format("修改成功！"),
                NavTabId = "User_List",
                Rel = "",
                CallbackType = "closeCurrent"
            }));
        }
        [DwzJsonException]
        public ActionResult Delete(string userId)
        {
            var bo = new Weixin.MP.BLL.Users.UsersBO();
            bo.DelUsers(userId);
            return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new DwzJsonResult()
            {
                StatusCode = DwzStatusCode.Ok,
                Message = string.Format("删除成功！"),
                NavTabId = "User_List",
                Rel = "",
                CallbackType = ""
            }));
        }

        public ActionResult Password()
        {
            var u = this.LoginUser;
            ViewBag.UserId = u.UserId;
            ViewBag.NickName = u.NickName;
            ViewBag.Email = u.Email;
            ViewBag.Phone = u.Phone;
            return View();
        }

        [HttpPost]
        [DwzJsonException]
        public ActionResult ChangePassword(string password, string nickName, string phone, string email)
        {
            var bo = new Weixin.MP.BLL.Users.UsersBO();
            var u = bo.GetByUserId(this.LoginUser.UserId);
            if (u == null)
            {
                return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new DwzJsonResult()
                {
                    StatusCode = DwzStatusCode.Error,
                    Message = string.Format("操作失败：{0}", "用户不存在！")
                }));
            }
            u.UserId = this.LoginUser.UserId;
            if (!string.IsNullOrEmpty(password))
            {
                u.Password = password.ToMD5();
            }
            u.NickName = nickName;
            u.Phone = phone;
            u.Email = email;
            bo.EditUsers(u);

            this.LoginUser = u;
            Session[Common.LoginUserSession] = this.LoginUser;
            return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new DwzJsonResult()
            {
                StatusCode = DwzStatusCode.Ok,
                Message = string.Format("修改成功！"),
                NavTabId = "User_Password",
                Rel = "",
                CallbackType = "closeCurrent"
            }));
        }
    }
}
